

import time
import os
import datetime
import subprocess
import openpyxl as op
import pandas as pd
import matplotlib.pyplot as plt

def screecingExcel(path,eccel_name,sheet_name):

    df = pd.read_excel(path+"\\"+eccel_name,sheet_name=sheet_name)
    df_name = df.drop_duplicates(subset=['name'])['name']
    for name in df_name.values :
        data = df.loc[df['name'].str.contains(name),['TotalPss']]
        data = data.reset_index()
        x = list(data.index)
        y = []
        for item in data.itertuples():
            y .append(int(item.TotalPss))
        imagePath = path +"\\"+"image"
        creatLogFile(imagePath)
        title = name
        xlable = "次数"
        ylable = "TotalPss"
        chart_name = name
        creat_chart(x,y,imagePath,chart_name,title,xlable,ylable)

def creat_chart(x,y,imagePath,chart_name,title,xlable,ylable):
    plt.figure(figsize=(8,4.5),dpi=(80))
    plt.title(title)
    plt.xlabel(xlable)
    plt.ylabel(ylable)
    plt.plot(x,y)
    plt.savefig("{}.png".format(imagePath+"\\"+chart_name),dpi = 300)


resulLog = [] ;
def walkFile(path,excel_name,sheet_name,sheetIdex):
    for root,dirs,files in os.walk(path):
        for f in files:
            anlysisLog(root,f)
    op_toexcel(sheet_name,sheetIdex,resulLog,path+"\\"+excel_name)


def anlysisLog(root,f):
    logfileobject = open(os.path.join(root,f),"r")
    try:
        logallText = logfileobject.read()
        datetime = logallText.split("\n")[0]
        logalllist = logallText.split("\\r\\n")
        cnt = 0
        for logitem in logalllist:
            if cnt == 5:
                linestr = ' '.join(logitem.split())
            elif cnt > 5 :
                if len(logitem) <= 5:
                    break
                logitem = logitem.replace("\\t"," ")
                linestr = ' '.join(logitem.split())
                linelist = linestr.split()
                # 3.1 的hidumper 只有10个标签
                if len(linelist) == 10 :
                    currPro = MemoryTrace()
                    currPro.pid = linelist[0]
                    currPro.name = linelist[1].replace("\\t","")
                    currPro.datetime = datetime
                    currPro.TotalPss = linelist[2].split("(")[0]
                elif len(linelist) > 10 :
                    # 3.2的 hidumper 有超过14个标签
                    currPro = MemoryTrace()
                    currPro.pid = linelist[0]
                    currPro.name = linelist[1].replace("\\t", "")
                    currPro.datetime = datetime
                    currPro.TotalPss = int(linelist[2].split("(")[0])
                else:
                    break
                resulLog.append(currPro)
            cnt += 1
    except Exception:
        print("读取文件错误:"+ f)
    finally:
        logfileobject.close()

def op_toexcel(sheet_name,index,data,file_name):
    if len(data) <= 0 :
        return
    wb = op.Workbook(file_name)
    ws = wb.create_sheet(sheet_name,index)
    heads = []
    for key in data[0].__dict__.keys():
        heads.append(key)
    ws.append(heads)
    for item in data:
        values = []
        for va in item.__dict__.values():
            values.append(va)
        ws.append(values)
    wb.save(file_name)

class MemoryTrace:
    def __init__(self):
        self.name = ""
        self.pid = ""
        self.datetime = ""
        self.TotalPss = "0"

# 间隔一定时间抓取指定次数的内存数据
def traceMem(Interval_time,Times,dlist,path):
    for off in range(Times):
        curr_time = datetime.datetime.now()
        for did in dlist:
            # 按照设备id 创建文件夹用于存储该设备的测试数据
            didname = path + "\\" + did
            creatLogFile(didname)
            logname = "hidumper_"+str(off).zfill(6)+".log"
            cmdlist = ["hdc_std.exe","-t",did,"shell","hidumper","--mem"]
            top_info = subprocess.Popen(cmdlist,stdout=subprocess.PIPE)
            out,err = top_info.communicate()
            f = open(didname + "/" + logname,'w+')
            f.write(str(curr_time) + "\n")
            f.write(str(out))
            f.close()
        time.sleep(Interval_time)

# 创建文件夹
def creatLogFile(path):
    if not os.path.exists(path):
        os.makedirs(path)
    return path

def main():
    # 抓取内存数据间隔时间,默认60秒
    Interval_time = 20
    # 抓取数据总次数  默认10次
    Times = 10
    # 测试设备ID列表 可以使用hdc list targets 指令查看设备ID ，支持多个设备
    dlist = ['7001005458323933328a260ed7ee3900']
    # 创建测试数据存储目录
    path = creatLogFile(os.getcwd()+"\\memlogs"+time.strftime("%Y%m%d_%H%M%S",time.localtime()))
    # 抓取内存数据
    traceMem(Interval_time,Times,dlist,path)
    # 读取设备测试数据并生成excel 和 内存曲线
    for sn in dlist:
        # 生成excel
        walkFile(path+"\\"+sn,"memcheck_result.xlsx","orgireport",0)
        # 生成内存曲线
        screecingExcel(path+"\\"+sn,"memcheck_result.xlsx","orgireport")


if __name__ == '__main__':
    # 测试前准备工作
    # 1、确保hdc_std.exe shell 能够正常执行
    # 2、确保python 安装 openpyxl，pandas，matplotlib 三个三方库
    main()
    # 测试过程中，提前结束或者出问题导致没有生成excel表格和内存曲线，可以注销main()函数，手动制定相关参数执行walkFile()和screecingExcel()函数
